***codee by Qianying Wang***
cd C:\STATA_DATA\energy_economics

***1. Spatial weight matrices***
spatwmat using w1.dta, name(w1) standardize // wij=0 or wij=1
spatwmat using w2.dta, name(w2) standardize // wij=1/(dij^2)
spatwmat using w3.dta, name(w3) standardize // wij=1/|GDPi-GDPj| 
spatwmat using w4.dta, name(w4) standardize // wij=1/((dij^2)|GDPi-GDPj|) 

** Spatial weight matrices for LM test***
use w1.dta, clear   
spatwmat using w1.dta, name(w1) standardize
spmat dta w1 var1-var31, norm(row) replace
drop var1-var31
set matsize 434            
mat TMAT = I(14)           
mat Wxt = TMAT#w1          
svmat Wxt
save Wxt.dta, replace      
spatwmat using Wxt.dta, name(W1) standardize

use w2.dta, clear           
spatwmat using w2.dta, name(w2) standardize
spmat dta w2 var1-var31, norm(row) replace      
drop var1-var31
set matsize 434             
mat TMAT = I(14)            
mat Wxt = TMAT#w2           
svmat Wxt
save Wxt.dta, replace       
spatwmat using Wxt.dta, name(W2) standardize

use w3.dta, clear           
spatwmat using w3.dta, name(w3) standardize
spmat dta w3 var1-var31, norm(row) replace      
drop var1-var31
set matsize 434             
mat TMAT = I(14)            
mat Wxt = TMAT#w3           
svmat Wxt
save Wxt.dta, replace       
spatwmat using Wxt.dta, name(W3) standardize

use w4.dta, clear          
spatwmat using w4.dta, name(w4) standardize
spmat dta w4 var1-var31, norm(row) replace      
drop var1-var31
set matsize 434             
mat TMAT = I(14)           
mat Wxt = TMAT#w4          
svmat Wxt
save Wxt.dta, replace       
spatwmat using Wxt.dta, name(W4) standardize


***2. Spatial autocorrelation analysis***
**Global spatial autocorrelation**
use data_RI_220510.dta,clear
spatgsa RI2019 RI2018 RI2017 RI2016 RI2015 RI2014 RI2013 RI2012 RI2011 RI2010 RI2009 RI2008 RI2007 RI2006, weights(w1) moran twotail
spatgsa RI2019 RI2018 RI2017 RI2016 RI2015 RI2014 RI2013 RI2012 RI2011 RI2010 RI2009 RI2008 RI2007 RI2006, weights(w2) moran twotail
spatgsa RI2019 RI2018 RI2017 RI2016 RI2015 RI2014 RI2013 RI2012 RI2011 RI2010 RI2009 RI2008 RI2007 RI2006, weights(w3) moran twotail
spatgsa RI2019 RI2018 RI2017 RI2016 RI2015 RI2014 RI2013 RI2012 RI2011 RI2010 RI2009 RI2008 RI2007 RI2006, weights(w4) moran twotail

**Local spatial autocorrelation**
use data_all_220510.dta, clear 
xtset province year   // n=31,t=14
xtdes 
gen lnRI=ln(RI)  
gen lnGF=ln(GF)       
gen lnGDP=ln(GDP)    
gen lnREIC=ln(REIC)   
gen lnCO2=ln(CO2)   
xtsum RI GF GDP REIC RES CO2 IPCII
xtmoran RI, wname(w3) morani(2006 2019) graph 
graph combine picture2006 picture2019, altshrink


***3. Nonspatial panel econometric model***
**Pooled OLS**
xtset province year 
reg lnRI lnGF lnGDP lnREIC RES lnCO2 IPCII
**Individual FE**
tsset province year 
xtreg lnRI lnGF lnGDP lnREIC RES lnCO2 IPCII, fe
**Time-period FE** 
tsset year province 
xtreg lnRI lnGF lnGDP lnREIC RES lnCO2 IPCII, fe                  
**Two-way FEooled OLS**
tsset province year 
xtreg lnRI lnGF lnGDP lnREIC RES lnCO2 IPCII i.year,fe  


***4. Spatial panel econometric model***
**LM-test** 
reg lnRI lnGF lnGDP lnREIC RES lnCO2 IPCII  
spatdiag, w(W1)
reg lnRI lnGF lnGDP lnREIC RES lnCO2 IPCII
spatdiag, w(W2)
reg lnRI lnGF lnGDP lnREIC RES lnCO2 IPCII
spatdiag, w(W3)
reg lnRI lnGF lnGDP lnREIC RES lnCO2 IPCII
spatdiag, w(W4)

**LR-test**
xsmle lnRI lnGF lnGDP lnREIC RES lnCO2 IPCII, wmat(w1) model(sdm) nolog noeffects fe type(both, leeyu)
est store sdm
xsmle lnRI lnGF lnGDP lnREIC RES lnCO2 IPCII, wmat(w1) model(sar) nolog noeffects fe type(both, leeyu)
est store sar
xsmle lnRI lnGF lnGDP lnREIC RES lnCO2 IPCII, emat(w1) model(sem) nolog noeffects fe type(both, leeyu)
est store sem
lrtest sdm sar // LR test for sar 
lrtest sdm sem // LR test for sem 

xsmle lnRI lnGF lnGDP lnREIC RES lnCO2 IPCII, wmat(w2) model(sdm) nolog noeffects fe type(both, leeyu)
est store sdm
xsmle lnRI lnGF lnGDP lnREIC RES lnCO2 IPCII, wmat(w2) model(sar) nolog noeffects fe type(both, leeyu)
est store sar
xsmle lnRI lnGF lnGDP lnREIC RES lnCO2 IPCII, emat(w2) model(sem) nolog noeffects fe type(both, leeyu)
est store sem
lrtest sdm sar // LR test for sar 
lrtest sdm sem // LR test for sem 

xsmle lnRI lnGF lnGDP lnREIC RES lnCO2 IPCII, wmat(w3) model(sdm) nolog noeffects fe type(both, leeyu)
est store sdm
xsmle lnRI lnGF lnGDP lnREIC RES lnCO2 IPCII, wmat(w3) model(sar) nolog noeffects fe type(both, leeyu)
est store sar
xsmle lnRI lnGF lnGDP lnREIC RES lnCO2 IPCII, emat(w3) model(sem) nolog noeffects fe type(both, leeyu)
est store sem
lrtest sdm sar // LR test for sar 
lrtest sdm sem // LR test for sem 

xsmle lnRI lnGF lnGDP lnREIC RES lnCO2 IPCII, wmat(w4) model(sdm) nolog noeffects fe type(both, leeyu)
est store sdm
xsmle lnRI lnGF lnGDP lnREIC RES lnCO2 IPCII, wmat(w4) model(sar) nolog noeffects fe type(both, leeyu)
est store sar
xsmle lnRI lnGF lnGDP lnREIC RES lnCO2 IPCII, emat(w4) model(sem) nolog noeffects fe type(both, leeyu)
est store sem
lrtest sdm sar // LR test for sar 
lrtest sdm sem // LR test for sem 

**Wald test**
xsmle lnRI lnGF lnGDP lnREIC RES lnCO2 IPCII, wmat(w1) model(sdm) nolog noeffects fe type(both, leeyu)
test [Wx]lnGF = [Wx]lnGDP = [Wx]lnREIC = [Wx]RES= [Wx]lnCO2 = [Wx]IPCII = 0  // Wald test for sar
testnl ([Wx]lnGF = -[Spatial]rho*[Main]lnGF) ([Wx]lnGDP = -[Spatial]rho*[Main]lnGDP) ([Wx]lnREIC = -[Spatial]rho*[Main]lnREIC) ([Wx]RES =-[Spatial]rho*[Main]RES) ([Wx]lnCO2 = -[Spatial]rho*[Main]lnCO2) ([Wx]IPCII = -[Spatial]rho*[Main]IPCII) // Wald test for sem

xsmle lnRI lnGF lnGDP lnREIC RES lnCO2 IPCII, wmat(w2) model(sdm) nolog noeffects fe type(both, leeyu)
test [Wx]lnGF = [Wx]lnGDP = [Wx]lnREIC = [Wx]RES= [Wx]lnCO2 = [Wx]IPCII = 0  // Wald test for sar
testnl ([Wx]lnGF = -[Spatial]rho*[Main]lnGF) ([Wx]lnGDP = -[Spatial]rho*[Main]lnGDP) ([Wx]lnREIC = -[Spatial]rho*[Main]lnREIC) ([Wx]RES =-[Spatial]rho*[Main]RES) ([Wx]lnCO2 = -[Spatial]rho*[Main]lnCO2) ([Wx]IPCII = -[Spatial]rho*[Main]IPCII) // Wald test for sem

xsmle lnRI lnGF lnGDP lnREIC RES lnCO2 IPCII, wmat(w3) model(sdm) nolog noeffects fe type(both, leeyu)
test [Wx]lnGF = [Wx]lnGDP = [Wx]lnREIC = [Wx]RES= [Wx]lnCO2 = [Wx]IPCII = 0  // Wald test for sar
testnl ([Wx]lnGF = -[Spatial]rho*[Main]lnGF) ([Wx]lnGDP = -[Spatial]rho*[Main]lnGDP) ([Wx]lnREIC = -[Spatial]rho*[Main]lnREIC) ([Wx]RES =-[Spatial]rho*[Main]RES) ([Wx]lnCO2 = -[Spatial]rho*[Main]lnCO2) ([Wx]IPCII = -[Spatial]rho*[Main]IPCII) // Wald test for sem

xsmle lnRI lnGF lnGDP lnREIC RES lnCO2 IPCII, wmat(w4) model(sdm) nolog noeffects fe type(both, leeyu)
test [Wx]lnGF = [Wx]lnGDP = [Wx]lnREIC = [Wx]RES= [Wx]lnCO2 = [Wx]IPCII = 0  // Wald test for sar
testnl ([Wx]lnGF = -[Spatial]rho*[Main]lnGF) ([Wx]lnGDP = -[Spatial]rho*[Main]lnGDP) ([Wx]lnREIC = -[Spatial]rho*[Main]lnREIC) ([Wx]RES =-[Spatial]rho*[Main]RES) ([Wx]lnCO2 = -[Spatial]rho*[Main]lnCO2) ([Wx]IPCII = -[Spatial]rho*[Main]IPCII) // Wald test for sem

**Spatial Dubin Model**
xsmle lnRI lnGF lnGDP lnREIC RES lnCO2 IPCII, wmat(w1) model(sdm) nolog effects fe type(both,leeyu)
xsmle lnRI lnGF lnGDP lnREIC RES lnCO2 IPCII, wmat(w2) model(sdm) nolog effects fe type(both,leeyu)
xsmle lnRI lnGF lnGDP lnREIC RES lnCO2 IPCII, wmat(w3) model(sdm) nolog effects fe type(both,leeyu)
xsmle lnRI lnGF lnGDP lnREIC RES lnCO2 IPCII, wmat(w4) model(sdm) nolog effects fe type(both,leeyu)


***5. Robustness check***
**Model 2: Knowledge stock**
gen lnKS=ln(KS)       
xsmle lnKS lnGF lnGDP lnREIC RES lnCO2 IPCII, wmat(w4) model(sdm) nolog effects fe type(both,leeyu)

**Model 3: lag 1-order**           
gen LlnGF = L.lnGF
gen LlnGDP = L.lnGDP
gen LlnREIC = L.lnREIC
gen LRES = L.RES
gen LlnCO2 = L.lnCO2
gen LIPCII = L.IPCII
keep if year != 2006
xsmle lnRI LlnGF LlnGDP LlnREIC LRES LlnCO2 LIPCII, wmat(w4) model(sdm) nolog effects fe type(both,leeyu)

















